package com.sakiko.stream.config;

import com.sakiko.stream.service.ChatAssistant;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * projectName: com.sakiko.stream.config
 *
 * @author: sakiko
 * time: 2025/8/27 23:21
 * description: 流式输出
 */
@Configuration
public class LLMConfig {

    // 普通对话接口 (没用上)
//    @Bean
//    public ChatModel chatModelQwen() {
//        return OpenAiChatModel.builder()
//                .apiKey(System.getenv("ALIBABA_BAILIAN_API_KEY"))
//                .modelName("qwen-plus")
//                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
//                .build();
//    }

    // 流式对话接口
    @Bean
    public StreamingChatModel streamingChatModel() {
        return OpenAiStreamingChatModel.builder()
                .apiKey(System.getenv("ALIBABA_BAILIAN_API_KEY"))
                .modelName("qwen-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

    // 注册 chatAssistant，使用 streamingChatModel模型做实现
    @Bean
    public ChatAssistant chatAssistant(StreamingChatModel streamingChatModel) {
        return AiServices.create(ChatAssistant.class, streamingChatModel);
    }
}
